namespace SimpleWebServices.Web
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using Xunit;

    public sealed class GoogleAnalyticsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<GoogleAnalytics>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("google")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            GoogleAnalytics actual = "<google tracker='abc123' domain='.example.com' />".Deserialize<GoogleAnalytics>();

            Assert.Equal<string>(".example.com", actual.Domain);
            Assert.Equal<string>("abc123", actual.Tracker);
        }

        [Fact]
        public void deserializeEmpty()
        {
            GoogleAnalytics actual = "<google />".Deserialize<GoogleAnalytics>();

            Assert.Null(actual.Domain);
            Assert.Null(actual.Tracker);
        }

        [Fact]
        public void serialize()
        {
            GoogleAnalytics obj = new GoogleAnalytics
            {
                Domain = ".example.com",
                Tracker = "abc123"
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/google[@tracker='abc123'][@domain='.example.com'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new GoogleAnalytics());
        }

        [Fact]
        public void prop_Domain()
        {
            Assert.True(new PropertyExpectations(typeof(GoogleAnalytics).GetProperty("Domain"))
                .XmlAttribute("domain")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Tracker()
        {
            Assert.True(new PropertyExpectations(typeof(GoogleAnalytics).GetProperty("Tracker"))
                .XmlAttribute("tracker")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void opImplicit_string_GoogleAnalytics()
        {
            GoogleAnalytics obj = new GoogleAnalytics
            {
                Tracker = "abc123"
            };

            string expected = obj.ToString();
            string actual = obj;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void opImplicit_string_GoogleAnalyticsNull()
        {
            string expected = null;
            string actual = null as GoogleAnalytics;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_ToString()
        {
            string expected =
                "<script type=\"text/javascript\">" +
                "var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");" +
                "document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));" +
                "</script>" +
                "<script type=\"text/javascript\">" +
                "try {" +
                "var pageTracker = _gat._getTracker(\"abc123\");" +
                "pageTracker._trackPageview();" +
                "} catch(err) {}</script>";

            GoogleAnalytics obj = new GoogleAnalytics
            {
                Tracker = "abc123"
            };

            string actual = obj.ToString();

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_ToString_whenDomain()
        {
            string expected =
                "<script type=\"text/javascript\">" +
                "var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");" +
                "document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));" +
                "</script>" +
                "<script type=\"text/javascript\">" +
                "try {" +
                "var pageTracker = _gat._getTracker(\"abc123\");" +
                "pageTracker._setDomainName(\".example.com\");" +
                "pageTracker._trackPageview();" +
                "} catch(err) {}</script>";

            GoogleAnalytics obj = new GoogleAnalytics
            {
                Domain = ".example.com",
                Tracker = "abc123"
            };

            string actual = obj.ToString();

            Assert.Equal<string>(expected, actual);
        }
    }
}